package com.saltedfish.tool.view.calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextUtils;
import com.haibin.calendarview.Calendars;
import com.haibin.calendarview.MonthView;

import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.util.ToastUtils;

/**
 * 高仿魅族日历布局
 * Created by huanghaibin on 2017/11/15.
 */

public class EndMonthView extends MonthView {

    private int mRadius;
    private Paint mSolarTermTextPaint = new Paint();

    private Calendars calendars;
    private Context mContext;

    public EndMonthView(Context context) {
        super(context);
        this.mContext = context;
        mSolarTermTextPaint.setColor(0xff489dff);
        mSolarTermTextPaint.setAntiAlias(true);
        mSolarTermTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onPreviewHook() {
        mSolarTermTextPaint.setTextSize(mCurMonthLunarTextPaint.getTextSize());
        mRadius = Math.min(mItemWidth, mItemHeight) / 6 * 2;
       // mSchemePaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onLoopStart(int x, int y) {

    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendars calendar, int x, int y, boolean hasScheme) {
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
       // canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
        if(calendars !=null){
            if(calendar.getTimeInMillis() < calendars.getTimeInMillis()){
                ToastUtils.showMessage(mContext,"请选择开始日期之后的日期");
                mSelectedPaint.setColor(0xffffffff);
                canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
            }else {
                if(!TextUtils.isEmpty(calendar.getScheme())){
                    if(calendar.getScheme().equals("开始")){
                        mSelectedPaint.setColor(0xffffffff);
                    }else {
                        mSelectedPaint.setColor(0xff489dff);
                    }
                }else {
                    mSelectedPaint.setColor(0xff489dff);
                }
                canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
            }
        }else {
            if(!TextUtils.isEmpty(calendar.getScheme())){
                if(calendar.getScheme().equals("开始")){
                    mSelectedPaint.setColor(0xffffffff);
                }else {
                    mSelectedPaint.setColor(0xff489dff);
                }
            }else {
                mSelectedPaint.setColor(0xff489dff);
            }
            canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
        }
        return true;
    }


    @Override
    protected void onDrawScheme(Canvas canvas, Calendars calendar, int x, int y) {
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        float baselineY = mTextBaseLine + y;
        if(!TextUtils.isEmpty(calendar.getScheme())){
            if(calendar.getScheme().equals("开始")){
                mSolarTermTextPaint.setColor(0xff489dff);
                calendars = calendar;
                canvas.drawText(calendar.getScheme(), cx,   y + mItemHeight,
                        !TextUtils.isEmpty(calendar.getScheme()) ? mSolarTermTextPaint : mSchemeLunarTextPaint);
            }else {

                mSelectTextPaint.setColor(0xff489dff);
                canvas.drawText(String.valueOf(calendar.getDay()),
                        cx,
                        baselineY,
                        mSelectTextPaint);
                canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
            }
        }
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendars calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
//            Log.e(isSelected+"---2-onDrawText->>",""+calendar.toStrings());
        boolean isInRange = isInRange(calendar);
        boolean isEnable = !updateSelectClickCalendars(calendar);
        if (isSelected) {
            if(calendars !=null){
                if(calendar.getTimeInMillis() < calendars.getTimeInMillis()){
                    mSelectTextPaint.setColor(0xff333333);
                }else {
                    mSelectTextPaint.setColor(0xffffffff);
                }
            }else {
                mSelectTextPaint.setColor(0xffffffff);
            }
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    mSelectTextPaint);

        } else if (hasScheme) {
//            XLog.e("--222--String.value------"+calendar.toStrings());
            if(!TextUtils.isEmpty(calendar.getScheme())){
                if(calendar.getScheme().equals("开始")){
                    mSelectTextPaint.setColor(0xff489dff);
                    mSelectedPaint.setColor(0xffffffff);
//                    canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
                }else {
                    mSelectTextPaint.setColor(0xffffffff);
                    mSelectedPaint.setColor(0xff489dff);
                }
                canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
                canvas.drawText(String.valueOf(calendar.getDay()),
                        cx,
                        baselineY,
                        mSelectTextPaint);
            }
        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth()  && isEnable ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }


    private boolean updateSelectClickCalendars(Calendars calendar){
        if(calendar.getTimeInMillis() < getCurrentDayMill()){
            return true;
        }else {
            return false;
        }
    }
    private long getCurrentDayMill(){
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.set(java.util.Calendar.HOUR,0);
        calendar.set(java.util.Calendar.MINUTE,0);
        calendar.set(java.util.Calendar.MILLISECOND,0);
        return calendar.getTimeInMillis();
    }
}
